/*-----------------------------------------------*
Name: Haqdarshak Pilot Intervention-Analysis
Date Created: 30 July, 2018
Date Last Modified: 28 April, 2021
Created by: Aaron Berman and modified by Daniela Paz and Saumya Mathur on Nov.13/2020 
Uses Data: endline_takeup_clean
Creates Data: Latex Tables
Description: Creates tables for the main analysis on endline takeup
*-------------------------------------------------*/



*..................................................
**Install user-written commands
foreach package in mdesc nmissing veracrypt {
     capture which `package'
	 if _rc==111 ssc install `package'
}
*..................................................
clear all
set more off
version 12.0
cap log close
pause off



//open log 
*cd "$logs"
*log using "endline_tables.smcl", replace


	use "$intermediate_data/endline_takeup_clean", replace
	******************************

	//make sure all screening treatments are defined in reference to control 
	foreach x of varlist treat_100 treat_50 treat_0 control_hh {
		replace `x' = 0 if missing(`x')
	}

	//generate dummies for village and stratum fixed effects
	tab village_id, gen(d_village_)
	tab stratum, gen(d_stratum_)
	replace administer_module10 = 0 if missing(administer_module10)

	//generate necessary interactions
	foreach x of varlist treat_100 treat_50 treat_0 {
		gen `x'_i = `x' * administer_module10
	}




	//one loop for each scheme 
	foreach x in "sukanya" "palanhaar" "postofficesavings" "ujjwalla" "oldagepension" "widowpension" "awas" "atalpension" {
		local z = ""
		if "`x'" == "postofficesavings"{
		cd "$Main"
		local z = "Table_7_8"
		}
		else if "`x'" == "sukanya"{
		cd "$Main"
		local z = "Table_9_A23"
		}
		else if "`x'" == "widowpension"{
		cd "$Appe"
		local z = "Table_A22"
		}
		else if "`x'" == "awas"{
		cd "$Appe"
		local z = "Table_A24"
		}
		else if "`x'" == "oldagepension"{
		cd "$Appe"
		local z = "Table_A25"
		}
		else if "`x'" == "ujjwalla"{
		cd "$Appe"
		local z = "Table_A26"
		}
		else if "`x'" == "palanhaar"{
		cd "$Appe"
		local z = "Table_A27"
		}
		else{
		cd "$latex"
		local z = "`x'_v1"
		}
		//regressions
		eststo clear 
		forv i = 1/5 {
			local depvar: word `i' of "heardof_`x'" "`x'_isuseful" "applied_`x'" "receiving_`x'" "recd_`x'_sincebl"
			eststo: reg `depvar' treat_100 treat_50 treat_0 d_stratum_*, robust 
		
			test treat_100=treat_50=treat_0
			estadd scalar pval `r(p)'
			
			summ `depvar' if control_hh == 1
			estadd scalar control_mean `r(mean)'
		}


		#delimit ;
		esttab *
			using "`z'.tex", booktabs label se
			drop(_cons d_stratum_*)
			b(%12.3f)
			se(%12.3f)
			starlevels(* .10 ** .05 *** .01)
			mlabels("Heard of Scheme" "Scheme is Useful" "Ever Applied for Scheme" "Ever Received Scheme" "Received Scheme Since Baseline")
			varlab(treat_100 "Free Screening" treat_50 "Half-Priced Screening" treat_0 "Full-Priced Screening")
			scalars("control_mean Control Mean" "pval /(p/)-value (Free=Half=Full)")
			nonotes
			fragment
			r2
			replace;

		#delimit cr 

		**VERSION WITH MODULE 10 CONTROLS
		//regressions
		eststo clear 
		forv i = 1/5 {
			local depvar: word `i' of "heardof_`x'" "`x'_isuseful" "applied_`x'" "receiving_`x'" "recd_`x'_sincebl"
			eststo: reg `depvar' treat_100 treat_50 treat_0 administer_module10 d_stratum_*, robust 
		
			test treat_100=treat_50=treat_0
			estadd scalar pval `r(p)'
			
			summ `depvar' if control_hh == 1
			estadd scalar control_mean `r(mean)'
		}


		//output to latex 
		cd "$latex"

		#delimit ;
		esttab *
			using "endline_regressions_`x'_module10controls.tex", booktabs label se
			drop(_cons d_stratum_* administer_module10)
			b(%12.3f)
			se(%12.3f)
			starlevels(* .10 ** .05 *** .01)
			mlabels("Heard of Scheme" "Scheme is Useful" "Ever Applied for Scheme" "Ever Received Scheme" "Received Scheme Since Baseline")
			varlab(treat_100 "Free Screening" treat_50 "Half-Priced Screening" treat_0 "Full-Priced Screening")
			scalars("control_mean Control Mean" "pval /(p/)-value (Free=Half=Full)")
			nonotes
			fragment
			r2
			replace;

		#delimit cr 

		**INTERACTION VERSION**
		//regressions
		eststo clear 
		forv i = 1/5 {
			local depvar: word `i' of "heardof_`x'" "`x'_isuseful" "applied_`x'" "receiving_`x'" "recd_`x'_sincebl"
			eststo: reg `depvar' treat_100_i treat_50_i treat_0_i treat_100 treat_50 treat_0 administer_module10 d_stratum_*, robust 

			test treat_100=treat_50=treat_0
			estadd scalar pval `r(p)'
			
			summ `depvar' if control_hh == 1 & administer_module10 == 0
			estadd scalar control_mean `r(mean)'
		}


		//output to latex 
		cd "$latex"

		#delimit ;
		esttab *
			using "endline_regressions_`x'_module10interactions.tex", booktabs label se
			drop(_cons d_stratum_*)
			b(%12.3f)
			se(%12.3f)
			starlevels(* .10 ** .05 *** .01)
			mlabels("Heard of Scheme" "Scheme is Useful" "Ever Applied for Scheme" "Ever Received Scheme" "Received Scheme Since Baseline")
			varlab(treat_100_i "Rec'd Baseline Module $/times$ Free Screening" treat_50_i "Rec'd Baseline Module $/times$ Half-Priced Screening" treat_0_i "Rec'd Baseline Module $/times$ Full-Priced Screening"
					 treat_100 "Free Screening" treat_50 "Half-Priced Screening" treat_0 "Full-Priced Screening" administer_module10 "Rec'd Baseline Module")
			scalars("control_mean Control Mean" "pval /(p/)-value (Free=Half=Full)")
			nonotes
			fragment
			r2
			replace;

		#delimit cr 
	}


	//version 2 where we don't count schemes with year == 1 as having received since bl 
	foreach x in "sukanya" "palanhaar" "ujjwalla" "oldagepension" "widowpension" "awas" "atalpension" {
	
		replace recd_`x'_sincebl = 1 if firstreceived_`x'_when == 3 & firstreceived_`x'_y == 1

		replace applied_`x' = 0 if missing(applied_`x')
		replace applied_`x' = 1 if applied_`x' == 2

		replace receiving_`x' = 0 if missing(receiving_`x')
		replace receiving_`x' = 1 if receiving_`x' == 2

		replace heardof_`x' = 0 if missing(heardof_`x')
	}

		replace recd_postofficesavings_sincebl = 1 if firstreceived_postofficesavings_ == 3 & v76 == 1



	local z = ""
	//one loop for each scheme 
	foreach x in "sukanya" "palanhaar" "postofficesavings" "ujjwalla" "oldagepension" "widowpension" "awas" "atalpension" {
		if "`x'" != "postofficesavings" {
			replace recd_`x'_sincebl = 0 if firstreceived_`x'_when == 3 & firstreceived_`x'_y == 1
		}
		else {
			replace recd_postofficesavings_sincebl = 0 if firstreceived_postofficesavings_ == 3 & v76 == 1
		}

		//regressions
		eststo clear 
		forv i = 1/5 {
			local depvar: word `i' of "heardof_`x'" "`x'_isuseful" "applied_`x'" "receiving_`x'" "recd_`x'_sincebl"
			eststo: reg `depvar' treat_100 treat_50 treat_0 d_stratum_*, robust 
		
			test treat_100=treat_50=treat_0
			estadd scalar pval `r(p)'
			
			summ `depvar' if control_hh == 1
			estadd scalar control_mean `r(mean)'
		}
		if "`x'" == "atalpension"{
		cd "$Appe"
		local z = "Table_A21"
		}
		else{
		cd "$latex"
		local z = "`x'_v2"
		}

		#delimit ;
		esttab *
			using "`z'.tex", booktabs label se
			drop(_cons d_stratum_*)
			b(%12.3f)
			se(%12.3f)
			starlevels(* .10 ** .05 *** .01)
			mlabels("Heard of Scheme" "Scheme is Useful" "Ever Applied for Scheme" "Ever Received Scheme" "Received Scheme Since Baseline")
			varlab(treat_100 "Free Screening" treat_50 "Half-Priced Screening" treat_0 "Full-Priced Screening")
			scalars("control_mean Control Mean" "pval /(p/)-value (Free=Half=Full)")
			nonotes
			fragment
			r2
			replace;

		#delimit cr 

	}	


	***ATTITUDES REGRESSIONS***
	//code baseline controls for 2 attitude questions 
	gen easy_to_apply_baseline = (difficulty_in_application == 3 | difficulty_in_application == 4) if !missing(difficulty_in_application)
	gen satsified_gp_baseline = (satisfied_grampanchayat == 1 | satisfied_grampanchayat == 2) if !missing(satisfied_grampanchayat)



	eststo clear 
	foreach x of varlist govt_needsofpoorfamilies govtscheme_difficultyinapplying govtscheme_satisfiedwithapplying satisfied_with_grampanchayat {
		replace `x' = . if `x' == 998
		recode `x' (2=1) (3=0) (4=0)
		tab `x'

		eststo: reg `x' treat_100 treat_50 treat_0 d_stratum_*, robust 
		test treat_100=treat_50=treat_0
		estadd scalar pval `r(p)'
		
		summ `x' if control_hh == 1
		estadd scalar control_mean `r(mean)'
	}

	//output to latex 
	cd "$latex"

	#delimit ;
	esttab *
		using "endline_attitudes.tex", booktabs label se
		drop(_cons d_stratum_*)
		b(%12.3f)
		se(%12.3f)
		starlevels(* .10 ** .05 *** .01)
		mlabels("Gov't Interested in Helping Poor Families" "Easy to Apply for Scheme" "Satisfied with Scheme App. Process" "Satisfied with GP")
		varlab(treat_100 "Free Screening" treat_50 "Half-Priced Screening" treat_0 "Full-Priced Screening")
		scalars("control_mean Control Mean" "pval /(p/)-value (Free=Half=Full)")
		nonotes
		fragment
		r2
		replace;

	#delimit cr


	***ATTITUDES REGRESSIONS WITH BASELINE CONTROLS FOR DIFFICULTY AND GP SATISFACTION	
	eststo clear 

	//difficulty applying for schemes 
	eststo: reg govtscheme_difficultyinapplying treat_100 treat_50 treat_0 easy_to_apply_baseline d_stratum_*, robust 
	test treat_100=treat_50=treat_0
	estadd scalar pval `r(p)'

	pause
	
	summ govtscheme_difficultyinapplying if control_hh == 1
	estadd scalar control_mean `r(mean)'

	//GP satisfaction
	eststo: reg satisfied_with_grampanchayat treat_100 treat_50 treat_0 satsified_gp_baseline d_stratum_*, robust 
	test treat_100=treat_50=treat_0
	estadd scalar pval `r(p)'

	pause 
	
	summ satisfied_with_grampanchayat if control_hh == 1
	estadd scalar control_mean `r(mean)'	


	//output to latex 
	cd "$latex"

	#delimit ;
	esttab *
		using "endline_attitudes_with_blcontrols.tex", booktabs label se
		drop(_cons d_stratum_* easy_to_apply_baseline satsified_gp_baseline)
		b(%12.3f)
		se(%12.3f)
		starlevels(* .10 ** .05 *** .01)
		mlabels("Easy to Apply for Scheme" "Satisfied with GP")
		varlab(treat_100 "Free Screening" treat_50 "Half-Priced Screening" treat_0 "Full-Priced Screening")
		scalars("control_mean Control Mean" "pval /(p/)-value (Free=Half=Full)")
		nonotes
		fragment
		r2
		replace;

	#delimit cr


	***ATTITUDES REGRESSIONS WITH MODULE 10 CONTROL***
	eststo clear 
	foreach x of varlist govt_needsofpoorfamilies govtscheme_difficultyinapplying govtscheme_satisfiedwithapplying satisfied_with_grampanchayat {
		eststo: reg `x' treat_100 treat_50 treat_0 administer_module10 d_stratum_*, robust 
		test treat_100=treat_50=treat_0
		estadd scalar pval `r(p)'
		
		summ `x' if control_hh == 1
		estadd scalar control_mean `r(mean)'
	}

	//output to latex 
	cd "$latex"

	#delimit ;
	esttab *
		using "endline_attitudes_module10 controls.tex", booktabs label se
		drop(_cons administer_module10 d_stratum_*)
		b(%12.3f)
		se(%12.3f)
		starlevels(* .10 ** .05 *** .01)
		mlabels("Gov't Interested in Helping Poor Families" "Easy to Apply for Scheme" "Satisfied with Scheme App. Process" "Satisfied with GP")
		varlab(treat_100 "Free Screening" treat_50 "Half-Priced Screening" treat_0 "Full-Priced Screening")
		scalars("control_mean Control Mean" "pval /(p/)-value (Free=Half=Full)")
		nonotes
		fragment
		r2
		replace;

	#delimit cr


	***WITH INTERACTION FOR BASELINE MODULE**
	eststo clear 
	foreach x of varlist govt_needsofpoorfamilies govtscheme_difficultyinapplying govtscheme_satisfiedwithapplying satisfied_with_grampanchayat {
		eststo: reg `x' treat_100_i treat_50_i treat_0_i treat_100 treat_50 treat_0 administer_module10 d_stratum_*, robust 
		test treat_100=treat_50=treat_0
		estadd scalar pval `r(p)'
		
		summ `x' if control_hh == 1 & administer_module10 == 0
		estadd scalar control_mean `r(mean)'
	}

	//output to latex 
	cd "$latex"

	#delimit ;
	esttab *
		using "endline_attitudes_module10interactions.tex", booktabs label se
		drop(_cons d_stratum_*)
		b(%12.3f)
		se(%12.3f)
		starlevels(* .10 ** .05 *** .01)
		mlabels("Gov't Interested in Helping Poor Families" "Easy to Apply for Scheme" "Satisfied with Scheme App. Process" "Satisfied with GP")
		varlab(treat_100_i "Rec'd Baseline Module $/times$ Free Screening" treat_50_i "Rec'd Baseline Module $/times$ Half-Priced Screening" treat_0_i "Rec'd Baseline Module $/times$ Full-Priced Screening"
				treat_100 "Free Screening" treat_50 "Half-Priced Screening" treat_0 "Full-Priced Screening" administer_module10 "Rec'd Baseline Module")
		scalars("control_mean Control Mean" "pval /(p/)-value (Free=Half=Full)")
		nonotes
		fragment
		r2
		replace;

	#delimit cr





